// 新添加都直角坐标层https://www.bbsmax.com/A/VGzlKxW1db/
import Graticule from 'ol/layer/Graticule'
import { Fill, Stroke, Style } from 'ol/style'
export default {
  data () {
    return {
    // oMap: null, // 地图对象
    // sSRS: 'EPSG:4326', // 空间参照系（Spatial Reference System) ?sRCS(Reference Coordinate System)
      coordLayer: null, // 网格层
      coordSize: 1000, // 坐标尺寸单位米
      coordSizePixel: 100, // 坐标格大小
      bShowCoordLayer: false
    }
  },
  // watch: {
  //   // watch
  // },
  // mounted () {
  //   // mounted
  // },
  methods: {
    /**
     * @description 创建gridLayer层，并添加到地图中
     * @param oMap {Object} 地图实例，
     * @return null
     */
    createCoordLayer (oMap = this.imap) {
      // 需要一个vector的layer来放置图标
      this.coordLayer = new Graticule({
      // the style to use for the lines, optional.
        strokeStyle: new Stroke({
          color: 'rgba(255,120,0,0.9)',
          width: 2,
          lineDash: [0.5, 4]
        }),
        targetSize: this.coordSizePixel,
        showLabels: true,
        visible: true,
        wrapX: false
      })
      // 添加图层名称
      this.coordLayer.set('name', 'coordLayer')
      // console.log(this.coordLayer, oMap)
      this.imap.addLayer(this.coordLayer)
      // 调整层级关系
      this.coordLayer.setZIndex(9)
    },

    /**
     * @description 销毁GridLayer层；
     * @param null
     * @return null
     */
    destroyCoordLayer (oMap = this.imap) {
      // console.log('dest')
      if (this.coordLayer) {
        this.imap.removeLayer(this.coordLayer)
      }
    },
    /**
     * @description 结算当前地图的网格尺寸；
     * @param null
     * @return null
     */
    setCoordSizex () {
      // console.log('setSize')
      const mapLevel = this.imap.getView().getZoom()
      let gridSize = this.coordSize
      if (mapLevel < 5 && gridSize < 1000000) gridSize = 1000000
      if (mapLevel < 10 && gridSize < 100000) gridSize = 100000
      if (mapLevel < 13 && gridSize < 50000) gridSize = 50000

      if (gridSize) {
        const a = this.imap.getPixelFromCoordinate([0, 0]) // 获取像素
        const b = this.imap.getPixelFromCoordinate([gridSize, 0]) // 获取像素
        this.coordSizePixel = b[0] - a[0]
      } else {
        this.coordSizePixel = 100
      }
    },
    /**
     * @description 重新绘制坐标层；
     * @param null
     * @return null
     */
    redrawCoord () {
      // console.log('redeaw')
      this.destroyCoordLayer()
      this.setCoordSizex()
      this.createCoordLayer()
    },
    /**
     *  @description 网格尺寸设置
     */
    getCoordSize () {
      return this.coordSize
    },
    setCoordSize (size) {
      this.coordSize = size
    },
    toggleCoord (isShowCoord) {
      // true 显示直角坐标
      if (isShowCoord) {
        this.bShowCoordLayer = true
        this.redrawCoord()
      } else {
        this.bShowCoordLayer = false
        this.destroyCoordLayer()
      }
    }
  }

}
